// pages/mycollect/mycollect.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    imgPath:app.globalData.imgPath,
    candel:false,
    selectAll:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/MembersFarvirate',
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (header.authorization && header.authorization != app.globalData.authorization) {
          app.globalData.authorization = header.authorization;
        }
        if (data.code == 200) {
          let list = data.data;
          list = list.map(function (item) {
            item.selected = false;
            return item;
          })
          self.setData({ list: data.data })
        }
      }
    }) 
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toedit: function () {//点击编辑
    this.setData({ candel: true })
  },
  del: function () {//点击删除
    let ids = [];
    let self = this;
    for (let i in this.data.list) {
      if (this.data.list[i].selected) {
        ids.push(this.data.list[i].id)
      }
    }
    if (ids.length == 0) {
      wx.showToast({
        title: '请勾选收藏',
        icon: 'none'
      })
      return false;
    }
    wx.request({
      url: app.globalData.reqPath + 'shop/MembersFarvirate/deletes',
      method: 'POST',
      data: {
        ids: ids
      },
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (header.authorization && header.authorization != app.globalData.authorization) {
          app.globalData.authorization = header.authorization;
        }
        if (data.code == 200) {
          wx.showToast({
            title: '删除成功',
            icon: 'none',
            success: function () {
              self.onLoad();
            }
          })
        }
      }
    })
  },
  finish: function () {
    this.setData({ candel: false })
  },
  //全选
  selectAll(e) {
    let selectAll = this.data.selectAll;    // 是否全选状态
    selectAll = !selectAll;
    let list = this.data.list;
    for (let i = 0; i < list.length; i++) {
      list[i].selected = selectAll;            // 改变所有商品状态
    }
    this.setData({
      selectAll: selectAll,
      list: list
    });
  },
  //点击选中
  selectList(e) {
    let index = e.currentTarget.dataset.index;    // 获取data- 传进来的index
    let list = this.data.list;
    let selected = list[index].selected;
    if (selected) {
      selected = false;
    } else {
      selected = true;
    }
    list[index].selected = selected;              // 改变状态
    let selectedlists = list.filter(function (item) {
      return item.selected
    })
    if (selectedlists.length == list.length) {
      this.setData({ selectAll: true })
    } else {
      this.setData({ selectAll: false })
    }
    this.setData({
      list: list
    });
  },
})